﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Linq.SqlClient;
using System.Collections.Generic;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Utility;

namespace WebHoiChoMuaBan.Controllers.Helpers
{
    public class CustomerHelper : HelperBase
    {
        private Customer customer = null;
        public Customer Customer
        {
            get { return customer; }
            set { customer = value; }
        }


        public CustomerHelper(int customerId)
            : base()
        {
            getCustomer(customerId);
        }

        public CustomerHelper()
            : base()
        {
        }
        public Customer getCustomer(int id)
        {
            this.customer = this.dataContext.Customers.SingleOrDefault(m => m.CusID == id);

            if (this.customer != null)
                this.customer.IsEdit = true;
            return this.customer;
        }

        public struct CustomerResult
        {
            public int id;
            public string logo;
            public string tencongty;
            public string dienthoai;
            public string diachi;
            public int? status;
        }
        public CustomerList getAllCustomers(int page, int rowPerPage)
        {
            IQueryable<CustomerResult> newMembers = from m in this.dataContext.Customers
                                                    orderby m.CusID ascending
                                                    select new CustomerResult
                                                     {
                                                         id = m.CusID,
                                                         logo = m.CusLogo,
                                                         tencongty = m.CusName,
                                                         dienthoai = m.CusCellPhone,
                                                         diachi = m.CusAddress,
                                                         status = m.CusStatus
                                                     };
            return CreateListCustomer(page, rowPerPage, newMembers);
        }

        private CustomerList CreateListCustomer(int page, int rowPerPage, IQueryable<CustomerResult> newCustomer)
        {
            List<CustomerResult> pagedNewCustomers = newCustomer.Skip((page - 1) * rowPerPage).Take(rowPerPage).ToList();

            int totalPages = (int)Math.Ceiling((double)newCustomer.Count() / rowPerPage);

            CustomerList lstMem = new CustomerList(totalPages);
            Customer mem;
            foreach (var i in pagedNewCustomers)
            {
                mem = new Customer();
                mem.CusID = i.id;
                mem.CusLogo = i.logo;
                mem.CusName = i.tencongty;
                mem.CusCellPhone = i.dienthoai;
                mem.CusAddress = i.diachi;
                mem.CusStatus = i.status;
                lstMem.Add(mem);
            }
            return lstMem;
        }

        public void saveCustomer(Customer cus)
        {
            if (cus.CusID == 0)
                this.dataContext.Customers.InsertOnSubmit(cus);
            else
            {
                Customer curr = getCustomer(cus.CusID);

                curr.CusAddress = cus.CusAddress;
                curr.CusCellPhone = cus.CusCellPhone;
                curr.CusEmail = cus.CusEmail;
                curr.CusFax = cus.CusFax;
                curr.CusLogo = cus.CusLogo;
                curr.CusName = cus.CusName;
                curr.CusPass = cus.CusPass;
                curr.CusPhone = cus.CusPhone;
                curr.CusStatus = cus.CusStatus;
            }
            Save();
        }

        public void DeleteCustomer(string id)
        {
            Customer en = (from e in this.dataContext.Customers where e.CusID == int.Parse(id) select e).FirstOrDefault();
            this.dataContext.Customers.DeleteOnSubmit(en);
            Save();
        }

        public Customer getCustomer(string id)
        {
            Customer en = (from e in this.dataContext.Customers where e.CusID == int.Parse(id) select e).FirstOrDefault();
            return en;
        }

        public List<Customer> getAllCustomerOnDefault()
        {
            List<Customer> result = (from e in this.dataContext.Customers orderby e.CusID descending select e).ToList<Customer>();
            return result;
        }
        //
        // Persistence

        public void Save()
        {
            this.dataContext.SubmitChanges();
        }

        internal void Refresh(Customer mem)
        {
            this.dataContext.Refresh(RefreshMode.OverwriteCurrentValues, mem);
        }
    }
}